package com.vcredit.creditcat.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.enumtype.StatusEnum;
import com.vcredit.creditcat.model.Resource;
import com.vcredit.creditcat.model.Role;
import com.vcredit.creditcat.model.User;
import com.vcredit.creditcat.persistence.ResourceMapper;
import com.vcredit.creditcat.persistence.RoleMapper;
import com.vcredit.creditcat.persistence.UserMapper;
/**
 * 后台权限springSecurity业务实现类
 *
 */
@Service
public class UserDetailService implements UserDetailsService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private ResourceMapper resourceMapper;
	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException, DataAccessException {

		User user = new User();
		user.setUserName(userName);
		user.setUserStatus(StatusEnum.AVAILABLE.getCode());
		List<User> list = userMapper.find(user);
		try{
			user = list.get(0);
		}catch (Exception e) {
			user = null;
		}
		if(user == null)
			return null;
		return user;
	}
}
